var express = require('express');
var mysql = require('mysql');

let query = {
    search: 'select * from `order` where userID=?',
    searchID: 'select * from `order` where orderID=?',
    searchMaxOrderID: 'select max(orderID) as oi from `order`',
    add: 'insert into `order`(userID,orderID,sellerID,sellerName,billSum,createTime,' +
        'orderState,cart,senderInfo,contactInfo) values (?,?,?,?,?,?,?,?,?,?)',
    del: 'delete from `order` where userID=? and orderID=?',
    update: 'update `order` set userID=?,orderID=?,sellerID=?,sellerName=?,billSum=?,' +
        'createTime=?,orderState=?,cart=?,deliverMessage=?' +
        ' where userID=? and orderID=?',
}

var search = function (userID,callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.search,[userID], function (err, result) {
        if (err) throw err;
        //打印查询结果
       callback(result);
       connection.end();
    })
}
var searchID = function (orderID,callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.searchID,[orderID], function (err, result) {
        if (err) throw err;
        //console.log(result[0]);
        let tmp=result[0].cart.split("|");
        for(i=0;i<tmp.length;i++){
            tmp[i] = JSON.parse(tmp[i])
        }
        result[0].cart = tmp;
        result[0].senderInfo = JSON.parse(result[0].senderInfo);
        result[0].contactInfo = JSON.parse(result[0].contactInfo);
        callback(result);
        connection.end();
    })
}
var searchMaxOrderID = function (callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log('数据库连接失败');
    });
    //console.log(query);
    connection.query(query.searchMaxOrderID, function (err, result) {
        if (err) throw err;
        //console.log('最大结果为：'+result);
        callback(result[0].oi);
        connection.end();
    })
}
var add = function (order){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    var arr = [];
    for(let i in order){
        arr.push(order[i]);
    }
    //console.log(arr);
    connection.query(query.add,arr, function (err, result) {
        if (err) throw err;
        connection.end();
    })

}
var del = function (userID,orderID){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.del,[userID,orderID], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}
var update = function (order){
//建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log(err);
    });
    connection.query(query.update,[], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}

exports.search = search;
exports.searchID = searchID;
exports.searchMaxOrderID = searchMaxOrderID;
exports.add = add;
exports.del = del;
exports.update = update;
